package com.bhs.sansonglogistics.ui.message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.SystemMessagesData;
import com.bhs.sansonglogistics.bean.event.MessageCenterRefresh;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.SystemMessagesAdapter;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 系统消息
 */
public class SystemMessagesActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private SystemMessagesAdapter mAdapter;
    private int page = 1;
    private String type;

    @Override
    public int initLayout() {
        return R.layout.activity_system_messages;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText(getIntent().getStringExtra("title"));
        type = getIntent().getStringExtra("type");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
        //阅读站内信
        networkRequest(netApi.read_message("logistics",type),this,88);
    }

    @Override
    public void initData() {
        mAdapter = new SystemMessagesAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));
        loadData();
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.layout_empty, null));

//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if(type.equals("1")) {
//                    startActivity(new Intent(mActivity, OrderDetailsActivity.class)
//                            .putExtra("deliver_sn", String.valueOf(mAdapter.getData().get(position).getAbout_id())));
//                }else if(type.equals("2")){
//                    startActivity(new Intent(mActivity, MyBillActivity.class));
//                }
//
//            }
//        });
    }

    Map<String, String> map = new ArrayMap<>();

    private void loadData() {
        map.put("page", String.valueOf(page));
        map.put("limit", "10");
        map.put("platform", "logistics");
        map.put("type", type);
        networkRequest(netApi.get_message_list(map), this);
    }


    @Override
    public void onClick(View view) {

    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if(requestCode==0) {
            SystemMessagesData dataBean = new Gson().fromJson(json, SystemMessagesData.class);
            mRefreshLayout.setRefreshing(false);
            if (dataBean.getCode().equals("1")) {
                if (page == 1) {
                    mAdapter.setNewData(dataBean.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(dataBean.getData().getList());
                }
                if (mAdapter.getData().size() >= dataBean.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            } else {
                ToastUtil.show(dataBean.getMsg());
            }
        }else {
            EventBus.getDefault().post(new MessageCenterRefresh());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}